package com.brige.service;

import java.util.*;

import org.apache.ibatis.annotations.Param;
import org.springframework.context.ApplicationContext;

import tool.MyApplicationContext;


import com.mcm.mapper.*;
import com.mcm.model.*;
import com.mcm.service.*;

import com.microhifo.mapper.*;
import com.microhifo.model.*;
import com.microhifo.service.*;

import tool.*;

/**
 * 功能：
 * 1）同步分销系统的注册用户 （分销系统注册用户 －－》microhifo的fx_user表）
 * 2）同步推荐客户（经纪人客户池－－》员工客户池）
 * 
 * 
 * 备注：
 * 单例模式
 * 
 * 工作流程：
 * 找出当日所有新增的客户
 * 
 * @author chz
 */
public class SyncService {
		// 单例模式 start
		private volatile static SyncService instance = null;
		
		private CustomerService cs;
		private FxUserService fus;
		private UserService us;
		
		
			private SyncService() {
			this.cs = CustomerService.getInstance();
			this.fus = FxUserService.getInstance();
			this.us = UserService.getInstance();

		}

		public static SyncService getInstance() {
			if (instance == null) {
				synchronized (SyncService.class) {// 1
					if (instance == null) {// 2
						instance = new SyncService();// 3
						
					}
				}
			}
			return instance;
		}
		
		
		// 单例模式 end
		private List<Customer> findNDaysCustomers(int days){
			DateUtil du = new DateUtil(days);
			return cs.getCustomerMapper().findNDaysCustomers(du.start, du.end);
		}
		
		private CustomerShop findByCustomerID(int CustomerID){
			return cs.getCustomerShopMapper().selectByCustomerID(CustomerID);
		}
		
		private User findByOpenID(String openID){
			return us.getUserMapper().selectByOpenID(openID);
		}
		
		// test
		public static void main(String[] args){
			SyncService ss = SyncService.getInstance();
			List<Customer> cs = ss.findNDaysCustomers(-100);
			System.out.println(cs.size());
			
			for(Customer c:cs){	
				System.out.print(c.getOpenid()+"###"+c.getNickname());
				User user = ss.findByOpenID(c.getOpenid());
				if(user!=null)
					System.out.print("###"+user.getTruename());
				
				CustomerShop shop= ss.findByCustomerID(c.getId());
				if(shop!=null)
					System.out.println("###"+shop.getChannelcode());
				
				
				
				System.out.println("");
			
			}
			
		}
		
}
